# coding:utf-8
import requests
import json


class English:
    def __init__(self):
        # 获取所有组数据URL - 有2种情况，分为带names和分names情况
        self.video_group_list_url = 'https://api.huilianyingyu.com/Sys/GetDictionariesList'

        # 单个分组数据
        self.video_list_url = 'https://api.huilianyingyu.com/Video/GetVideoListNot'

        # 是否开启代理
        self.is_proxies = True
        self.proxies = {'http': 'http://localhost:8888', 'https': 'http://localhost:8888'}

    # 执行入口
    def run_data(self):
        home_request_param = {
            'grouping': '视频分组',
            'names': '靶向练习,简单英语,生活词汇,替换练习,新概念,循环练习,多情景语法,多情景语块,多情景句型,多情景单词,多图片单词,发音练习,ZM,SA,MC,LLE,ETP,EP,DE'
        }
        home_group_list = self.get_group_list(home_request_param)
        for group_data in home_group_list:
            group_id = group_data.get('id')
            # 根据PID，再获取子目录数据
            pid_request_param = {'grouping': '视频分组', 'pid': group_id}
            child_group_list = self.get_group_list(pid_request_param)
            if child_group_list:
                for child_group_data in child_group_list:
                    child_data_id = child_group_data.get('id')
                    # 获取视频数据列表
                    video_list_request_param = {
                        "gid": child_data_id,
                        "page": {
                            "index": 1,
                            "rows": 100000
                        }
                    }
                    child_result = self.get_video_list(video_list_request_param)
                    print(child_result)
            # 获取视频数据列表
            video_list_request_param = {
                "gid": group_id,
                "page": {
                    "index": 1,
                    "rows": 100000
                }
            }
            result = self.get_video_list(video_list_request_param)
            print(result)

    # 获取所有组数据，分组分为2种，1通过不同的names请求；2通过pid进行请求
    def get_group_list(self, request_param):
        if self.is_proxies:
            r = requests.get(url=self.video_group_list_url, params=request_param, headers=self.create_headers(), proxies=self.proxies, verify=False)
        else:
            r = requests.get(url=self.video_group_list_url, params=request_param, headers=self.create_headers())
        return r.json().get('data')

    # 获取某个分组下的列表数据
    def get_video_list(self, request_param):
        if self.is_proxies:
            r = requests.post(url=self.video_list_url, json=request_param, headers=self.create_headers(), proxies=self.proxies, verify=False)
        else:
            r = requests.post(url=self.video_list_url, json=request_param, headers=self.create_headers())

        return r.json().get('data')

    # 下载单个视频数据
    def download_video(self):
        pass

    # 生成请求 headers
    @staticmethod
    def create_headers():
        headers = {
            'Accept': 'application/json, text/plain, */*',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'keep-alive',
            'Host': 'api.huilianyingyu.com',
            'Origin': 'https://www.huilianyingyu.com',
            'Referer': 'https://www.huilianyingyu.com/',
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-site',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
            'id': '109027',
            'nonce': '31924139',
            'sec-ch-ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'signature': 'af8a4a1d71a0c7d913ee065274c720cc',
            'timestamp': '1713637466000000'
        }
        return headers
